#!/usr/bin/python3.8
# -*- coding: utf-8 -*-
# @Time    : 2021/3/6 6:49 下午 
# @Author  : zhaohexin
# @Email   : 1483643520@xxx.com
# @File    : model.py
# @Software: PyCharm 
# @FileDescription：部门模型

from DatabaseOperation.BaseModel import BaseModel
from sqlalchemy import Column, VARCHAR, Integer, BigInteger, Boolean


class Depts(BaseModel):
    """
    部门表
    """
    __tablename__ = "sys_dept"
    __table_args__ = ({"comment": "部门表"})

    pid = Column(BigInteger, nullable=True, default="", comment="上级部门ID")
    sub_count = Column(Integer, nullable=True, default=0, comment="子部门数")
    name = Column(VARCHAR(255), nullable=False, comment="部门名称")
    sort = Column(Integer, nullable=True, default=0, comment="排序")
    enabled = Column(Boolean, nullable=False, comment="启用状态")

    # 把SQLAlchemy查询对象转换成字典
    def to_dict(self):
        return {
            "deptSort": self.sort,
            "id": self.id,
            "enabled": self.enabled,
            "name": self.name,
            "label": self.name,
            "leaf": False if self.sub_count and self.sub_count > 0 else True,
            "hasChildren": True if self.sub_count and self.sub_count > 0 else False,
            "subCount": self.sub_count,
            "createTime": self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            "updateTime": self.update_time.strftime('%Y-%m-%d %H:%M:%S'),
            "createBy": self.create_by
        }
